﻿Imports System.Security.Permissions

<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
Public Class Home
    Inherits System.Web.UI.Page

    Public Shared dbmanager As DBManager = New DBManager
    Public Shared bannerImages As ArrayList = New ArrayList

    'To set masterpage 
    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        'Check if user is null
        If (Session("user") IsNot Nothing) Then
            'Retrieve userAcc details from session
            Dim userLoggedIn As userAcc = Session("user")
            'Check if user is admin, assign masterpage respectively
            If userLoggedIn.isAdmin Then
                Me.MasterPageFile = "~/MasterPage/Admin.Master"
            Else
                Me.MasterPageFile = "~/MasterPage/User.Master"
            End If
        Else
            'For anonymous users
            Me.MasterPageFile = "~/MasterPage/User.Master"
            'Enable this line to prevent anonymous users from entering this page
            ' Response.Redirect("~/Login.aspx")
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        initializeSearch()
        'Display top 3 popular images if is not postback
        If (Not IsPostBack) Then
            initializeImagePopular()
            'This initializes the front page banner
            Dim imageCount = 1
            bannerImages = dbmanager.getFrontPageBanner()
            For x As Integer = 0 To bannerImages.Count
                If (x Mod 2 = 1) Then
                    Dim imageAdv As ImageButton = imgAdvertisement1.Parent.FindControl("imgAdvertisement" + imageCount.ToString())
                    imageCount = imageCount + 1
                    imageAdv.ImageUrl = "BannerHttpHandler.ashx?id=" + bannerImages(x).ToString()
                    imageAdv.CommandArgument = bannerImages(x).ToString()
                    Dim outletID = dbmanager.getOutletIDBanner(Convert.ToInt32(imageAdv.CommandArgument))
                    If outletID = 0 Then
                        imageAdv.PostBackUrl = "AdvertiseOverview.aspx"
                    Else
                        imageAdv.PostBackUrl = "OutletReview.aspx?id=" + outletID.ToString()
                    End If

                End If
            Next
        End If

    End Sub


    Protected Sub initializeSearch()
        'To fill up the "most popular search"
        Dim topSearch As ArrayList = dbmanager.topSearch()
        lbSearch1.Text = topSearch(0).ToString
        lbSearch1.PostBackUrl = "SearchResults.aspx?s=" + topSearch(0)
        lbSearch2.Text = topSearch(1)
        lbSearch2.PostBackUrl = "SearchResults.aspx?s=" + topSearch(1)
        lbSearch3.Text = topSearch(2)
        lbSearch3.PostBackUrl = "SearchResults.aspx?s=" + topSearch(2)
        lbSearch4.Text = topSearch(3)
        lbSearch4.PostBackUrl = "SearchResults.aspx?s=" + topSearch(3)
        lbSearch5.Text = topSearch(4)
        lbSearch5.PostBackUrl = "SearchResults.aspx?s=" + topSearch(4)
        lbSearch6.Text = topSearch(5)
        lbSearch6.PostBackUrl = "SearchResults.aspx?s=" + topSearch(5)
        lbSearch7.Text = topSearch(6)
        lbSearch7.PostBackUrl = "SearchResults.aspx?s=" + topSearch(6)
        lbSearch8.Text = topSearch(7)
        lbSearch8.PostBackUrl = "SearchResults.aspx?s=" + topSearch(7)
    End Sub

    'Display images of top 3 most popularbusinesses
    Protected Sub initializeImagePopular()
        Dim imageArray As ArrayList = dbmanager.getTopPopularBusiness()
        'Set image url
        imageFront1.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(0)
        imageFront2.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(1)
        imageFront3.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(2)

        'Get outlet url to redirect to 
        Dim outletID1 = dbmanager.getOutletImageID(imageArray(0))
        Dim outletID2 = dbmanager.getOutletImageID(imageArray(1))
        Dim outletID3 = dbmanager.getOutletImageID(imageArray(2))

        'Set postback url
        imageFront1.PostBackUrl = "OutletReview.aspx?id=" + outletID1.ToString()
        imageFront2.PostBackUrl = "OutletReview.aspx?id=" + outletID2.ToString()
        imageFront3.PostBackUrl = "OutletReview.aspx?id=" + outletID3.ToString()
        lbImageFront1.PostBackUrl = "OutletReview.aspx?id=" + outletID1.ToString()
        lbImageFront2.PostBackUrl = "OutletReview.aspx?id=" + outletID2.ToString()
        lbImageFront3.PostBackUrl = "OutletReview.aspx?id=" + outletID3.ToString()

        'Set text of the labels
        lbImageFront1.Text = "#1 - " + dbmanager.getBusinessName(outletID1)
        lbImageFront2.Text = "#2 - " + dbmanager.getBusinessName(outletID2)
        lbImageFront3.Text = "#3 - " + dbmanager.getBusinessName(outletID3)

        'Set linkbutton colors 
        lbImagePopular.ForeColor = Drawing.Color.Black
        lbImageNewest.ForeColor = Drawing.Color.Blue

    End Sub

    'Display images of top 3 most popularbusinesses
    Protected Sub initializeImageNewest()
        Dim imageArray As ArrayList = dbmanager.getTopNewestBusiness()
        'Set image url
        imageFront1.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(0)
        imageFront2.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(1)
        imageFront3.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(2)

        'Get outlet url to redirect to 
        Dim outletID1 = dbmanager.getOutletImageID(imageArray(0))
        Dim outletID2 = dbmanager.getOutletImageID(imageArray(1))
        Dim outletID3 = dbmanager.getOutletImageID(imageArray(2))

        'Set postback url
        imageFront1.PostBackUrl = "OutletReview.aspx?id=" + outletID1.ToString()
        imageFront2.PostBackUrl = "OutletReview.aspx?id=" + outletID2.ToString()
        imageFront3.PostBackUrl = "OutletReview.aspx?id=" + outletID3.ToString()
        lbImageFront1.PostBackUrl = "OutletReview.aspx?id=" + outletID1.ToString()
        lbImageFront2.PostBackUrl = "OutletReview.aspx?id=" + outletID2.ToString()
        lbImageFront3.PostBackUrl = "OutletReview.aspx?id=" + outletID3.ToString()

        'Set text of the labels
        lbImageFront1.Text = "#1 - " + dbmanager.getBusinessName(outletID1)
        lbImageFront2.Text = "#2 - " + dbmanager.getBusinessName(outletID2)
        lbImageFront3.Text = "#3 - " + dbmanager.getBusinessName(outletID3)

        'Set linkbutton colors 
        lbImagePopular.ForeColor = Drawing.Color.Blue
        lbImageNewest.ForeColor = Drawing.Color.Black
    End Sub

    'If user wants to show top 3 newest businesses
    Protected Sub lbImageNewest_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lbImageNewest.Click
        initializeImageNewest()
    End Sub

    'If user wants to show top 3 most popular businesses
    Protected Sub lbImagePopular_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lbImagePopular.Click
        initializeImagePopular()
    End Sub

    'WebMethod for javascript to call, to track banner impresssions
    <System.Web.Services.WebMethod()> _
    Public Shared Function triggerChangeImage(ByVal x) As Integer
        Dim y = x("imageID")
        Try
            Dim imageID = bannerImages((y * 2) + 1)
            dbmanager.incrementImpression(imageID)
        Catch ex As Exception
        End Try
        Return (Nothing)
    End Function

    Protected Shared Sub trackChange()
        'Dim thediv As HtmlGenericControl = DirectCast(Page.FindControl("divid"), HtmlGenericControl)
    End Sub

    Protected Sub BannerClick(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Dim btnImage As ImageButton = sender
        dbmanager.incrementClicks(btnImage.CommandArgument)
    End Sub
End Class